package com.zyh.utils;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author zyh
 * @Date 2024/8/9 9:57
 * @Description CyclicBarrier 加法计数器
 */

public class CyclicBarrierDemo1 {
    public static void main(String[] args) {
        //创建加法计数器
        CyclicBarrier cyclicBarrier = new CyclicBarrier(108, () -> System.out.println("收集成功！兑换小霸王学习机"));
        //创建收集任务
        Runnable task = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "收集成功");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }
        };
        //创建108个线程
        for (int i = 1; i <= 108; i++) {
            new Thread(task, "梁山好汉" + i).start();
        }
    }
}
